package com.darknight.ftp.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

/**
 * Created by DarKnight on 2017/5/17.
 */
public class ClientUtils {
    public static boolean uploadFile(InputStream inputStream) {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            ftp.connect("localhost", 2121);//连接FTP服务器
            //如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
            ftp.login("admin", "admin");//登录
            int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();

                return success;
            }
            // 初次切换目录时以根 ftpserver.user.admin.homedirectory 为起点
            // 之后每次切换都以前一次切换后的目录为新起点
            boolean code = ftp.changeWorkingDirectory("/test");
            ftp.storeFile("test0002.txt", inputStream);


            ftp.logout();
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException e) {

                }
            }
        }

        return success;
    }

    public static boolean uploadFile(MultipartFile uploadFile) {
        boolean success = false;
        InputStream inputStream;

        try {
            inputStream = uploadFile.getInputStream();
            success = ClientUtils.uploadFile(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return success;
    }

    public static boolean uploadFile() {
        boolean success = false;
        FileInputStream input = null;
        try {
            input = new FileInputStream(new File("D:/Server/FtpServer/uploadData/0001.txt"));
            success = uploadFile(input);
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return success;
    }

    public String springUploadFileList(String uploadPath, List<MultipartFile> fileList) {

        if(fileList != null && !fileList.isEmpty()) {
            // 获取上传目录，没有时创建该目录
            File targetDir = FileUtils.getFile(uploadPath);
            if(!targetDir.exists()) {
                targetDir.mkdirs();
            }

            // 循环保存上传文件
            for(MultipartFile multipartFile : fileList) {
                if(!multipartFile.isEmpty()) {
                    try {
                        // 获取文件对象，没有时创建该对象
                        File targetFile = FileUtils.getFile(uploadPath + "/" + multipartFile.getOriginalFilename());
                        if(!targetFile.exists()) {
                            targetFile.createNewFile();
                        }
                        multipartFile.transferTo(targetFile);
                    } catch(IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return null;
    }
}
